package com.easyfun.healthmagicbox.bluetooth.ycgi;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Message;
import android.util.Log;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.bluetooth.BTMessageHandler;
import com.easyfun.healthmagicbox.inputviews.HardwareInputActivity;
import com.easyfun.healthmagicbox.ormlite.HealthInputTypeEnum;
import com.easyfun.healthmagicbox.pojo.HealthInputData;
import com.easyfun.healthmagicbox.util.HealthDateUtil;

public class YiChengGIHandler extends BTMessageHandler {
	YiChengGIParser parser = new YiChengGIParser();
	
	public YiChengGIHandler(String personID, Context context,
			HardwareInputActivity _parent) {
		super(personID, context, _parent);
	}

	private static final String TAG = YiChengGIHandler.class.getSimpleName();

	@Override
	protected void OnMessageWrite(byte[] msg, int len) {
		
	}

	@Override
	protected void OnMessageRead(byte[] msgbytes, int len) {
		YiChengGIMessage msg = parser.parse(msgbytes, len);
		if (msg == null)
		{
			return;
		}
		switch (msg.getMsgType())
		{
		case WORKFLOW_RESPONSE:
			Log.i(TAG,"Receive Step:"+String.valueOf(msg.getWorkFlowStep()));
			byte[] tmp = YiChengGIMessage.retrieveDataCommand(1);
			super.write(tmp);
			break;
		case DATA_RESPONSE:
			Log.i(TAG,"Receive Data:");
			int sugar = msg.getBloodsugar();
			if (sugar > 0)
			{
				saveData(sugar,msg.getDatetime());
			}
		default:
			break;
			
		}
		
	}
	
	protected void saveData(int suger, long datetime)
	{
		HealthInputData hid1 = new HealthInputData();
		hid1.setValueTime(datetime);
		hid1.setPersonID(m_personid);
		int big = suger / 10;
		int afterdot = suger % 10;
		hid1.setValueData(String.valueOf(big) + "." + String.valueOf(afterdot));
		hid1.setValueType(HealthInputTypeEnum.XUETANG_CANHOU.toInteger());		
		sound(hid1);
		
		List<HealthInputData> hids = new ArrayList<HealthInputData>();
		hids.add(hid1);
		Message message = new Message();   
        message.what = parent.BT_RESULT_ARRIVED;   
        message.obj = hids;
		parent.uiHandler.sendMessage(message);	
	}

}
